package com.cyq.day06;

import java.util.Scanner;

public class HomeWork01 {
    public static void main(String[] args) {
        //猜数字游戏：
        //系统产生一个不重复的4位数如3267
        //没猜对会给相应提示：你猜的数字和系统生成的数字有几个位置对了或者值对了
        //A表示值和位置都对了，B表示值对了位置不对，如猜测1239会提示1A1B。4A0B为全对
        //1.系统生成不重复四位数
        int[] ary = new int[4];
        for (int i = 0; i < ary.length; i++) {
            ary[i] = (int) (Math.random() * 10);
            //保证四位数不相同
            for (int j = 0; j < i; j++) {
                if (ary[i] == ary[j]) {
                    i--;
                    break;
                }
            }
        }
        for (int a : ary
        ) {
            System.out.print(a);
        }
        System.out.println();
        //2.用户输入四位数字
        for (int a = 0; a < 5; a++) {
            Scanner sc = new Scanner(System.in);
            int[] intAry = new int[4];
            while (true) {
                System.out.println("请输入你猜测的四位数字");
                String userNum = sc.next();
                //将用户输入的数字转换为字符
                char[] newAry = userNum.toCharArray();
                intAry = new int[newAry.length];
                boolean isRepeat = false;
                for (int i = 0; i < newAry.length; i++) {
                    intAry[i] = (int) newAry[i] - 48;
                    for (int j = 0; j < i; j++) {
                        if (intAry[i] == intAry[j]) {
                            System.out.println("不应该输入两个相同的数字");
                            isRepeat = true;
                            break;
                        }
                    }
                }
                if (!(newAry.length == 4)) {
                    System.out.println("输入的数字应该是4位");
                    isRepeat = true;
                }
                if (!isRepeat) {
                    break;
                }
            }
            for (int i : intAry
            ) {
                System.out.print(i);
            }
            //判断是否猜对并给出NANB的提示
            int aFlag = 0;
            int bFlag = 0;
            for (int i = 0; i < ary.length; i++) {
                if (ary[i] == intAry[i]) {
                    aFlag++;
                }
                for (int j = 0; j < intAry.length; j++) {
                    if (ary[i] == intAry[j]) {
                        bFlag++;
                    }
                }
            }
            if (aFlag == 4) {
                System.out.println("猜对了");
                break;
            }
            System.out.println("你的猜测结果为" + aFlag + "A" + (bFlag - aFlag) + "B");
            System.out.println("你还能猜" + (4 - a) + "次");
        }
    }
}